package com.muy.redis.view.window;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowFactory;
import com.intellij.openapi.wm.ex.ToolWindowEx;
import com.muy.redis.common.tree.actions.AddSubTreeNodeTopDbAction;
import com.muy.redis.common.tree.actions.DeleteCurrentTreeNodeTopDbAction;
import com.muy.redis.common.tree.actions.TreeCollapseAction;
import com.muy.redis.common.tree.actions.TreeExpandAction;
import com.muy.redis.view.component.MrRedisComponent;
import com.muy.redis.view.component.MrRedisTab;
import org.apache.commons.compress.utils.Lists;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @Author jiyanghuang
 * @Date 2023/9/15 20:08
 */
public class MrRedisMainWindowFactory implements ToolWindowFactory {

    public static Project project;

    @Override
    public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
        project = project;
        toolWindow.setToHideOnEmptyContent(true);
        MrRedisComponent component = MrRedisComponent.getInstance(project);
//        component.setToolWindow(toolWindow);
        MrRedisTab mrRedisTab = component.initComponent(toolWindow);
        initAction(toolWindow, mrRedisTab);
    }

    private void initAction(ToolWindow toolWindow, MrRedisTab mrRedisTab) {
        List<AnAction> actions = Lists.newArrayList();
        ToolWindowEx ex = (ToolWindowEx) toolWindow;
        AddSubTreeNodeTopDbAction addSubTreeNodeTopDbAction = new AddSubTreeNodeTopDbAction(mrRedisTab.getMrRedisTree());
        DeleteCurrentTreeNodeTopDbAction deleteCurrentTreeNodeTopDbAction = new DeleteCurrentTreeNodeTopDbAction(mrRedisTab.getMrRedisTree());
        TreeCollapseAction treeCollapseAction = new TreeCollapseAction(mrRedisTab.getMrRedisTree().jTree());
        TreeExpandAction treeExpandAction = new TreeExpandAction(mrRedisTab.getMrRedisTree().jTree());
        actions.add(addSubTreeNodeTopDbAction);
        actions.add(deleteCurrentTreeNodeTopDbAction);
        actions.add(treeCollapseAction);
        actions.add(treeExpandAction);
        toolWindow.setTitleActions(actions);
    }
}
